package serviceset2

import grails.gorm.transactions.Rollback
import grails.testing.mixin.integration.Integration
import groovy.json.JsonSlurper
import org.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.mock.web.MockHttpServletResponse
import org.springframework.web.context.WebApplicationContext
import spock.lang.Shared
import spock.lang.Specification

@Integration
@Rollback
class SampleApi2ControllerSpec extends Specification {

    @Autowired
    WebApplicationContext ctx

    @Shared
    GrailsWebRequest webRequest

    void setup() {
        webRequest = grails.util.GrailsWebMockUtil.bindMockWebRequest(ctx)
        webRequest.currentRequest.asyncSupported = true
    }

    void cleanup() {
        org.springframework.web.context.request.RequestContextHolder.resetRequestAttributes()
    }

    void "test async controller"() {
        when:
        new SampleApi2Controller().index(dependency)

        then:
        webRequest.currentResponse.status == 200

        when:
        def jsonResp = new JsonSlurper().parseText((webRequest.currentResponse as MockHttpServletResponse).getContentAsString())

        then:
        jsonResp instanceof Map

        when:
        def result = jsonResp as Map

        then:
        result.provider == "sampleApi2@serviceSet2"
        result.dependency == dependency

        where:
        dependency << ["feign", "restTemplate", ""]
    }
}
